package assignmentonepenguins

import com.security.User
import com.security.UserRole
import com.security.Role

class RegistrationController {

    def index() {

        redirect(action: "createUser", params: params)
    }


    def createUser() {

        def customerInstance = new Customer(params)
        customerInstance.setDateCreated(new Date())
        if (!customerInstance.save(flush: true)) {

            render(view: "createUser", model: [customerInstance: customerInstance])
//            flash.message = message(code:"User already exists !")
            return
        }
        def userRole = Role.findByAuthority("ROLE_USER")

        def testUser1 = new User(username: params.username, enabled: true, password: params.password)
        testUser1.save(flush: true)
//        UserRole ur = new UserRole()
//        ur.setUser(testUser1)
//        ur.setRole(userRole)
//        ur.save(flush: true)
        UserRole.create(testUser1, userRole, true)

//        UserRole.create testUser1, userRole, true


        flash.message = message(code: 'default.created.message', args: [message(code: 'customer.label', default: 'Customer'), customerInstance.id])
        redirect(controller: "login")

    }
}